package top.xia17.plugins.fast.common.spring.converter;


import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import top.xia17.plugins.fast.common.exceptions.BadRequestException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 转换日期
 * @author xia17
 * @date 2019/5/25 14:52
 */
@Slf4j
public class LocalDateConverter implements Converter<String,LocalDate> {

    /**
     * String to LocalDate
     * @param s 字符串
     * @return LocalDate
     */
    @Override
    public LocalDate convert(@NonNull String s) {
        String nullStr = "null";
        if ("".equals(s) || nullStr.equalsIgnoreCase(s)){
            return null;
        }
        try {
            return LocalDate.parse(s, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }catch (RuntimeException e){
            log.error("参数转换Date异常："+ e.getMessage());
            throw new BadRequestException("时间转换异常");
        }
    }
}
